package com.superlc.ssm.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.support.DefaultSubjectContext;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

/**
 * @ProjectName: ssm$
 * @Package: com.superlc.ssm.utils$
 * @ClassName: ProjectUtils$
 * @Description: 工具类
 * @Author: super
 * @CreateDate: 2018/8/16$ 15:52$
 * @UpdateUser: 更新者
 * @UpdateDate: 2018/8/16$ 15:52$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class ProjectUtils {

    public static Object hashCredentials(String hashAlgorithName,String data,String salt,int hashIterations){
        Object obj = new SimpleHash(hashAlgorithName,data, salt, hashIterations);
        return obj;
    }

    public static boolean isAuthenticated(HttpSession session){
        boolean  status = false ;
        try{

            Object obj = session.getAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY);
            if(obj != null){
                status = (Boolean) obj;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return status;
    }

    public static void object2Json(Object obj, Writer out){
        ObjectMapper mapper = new ObjectMapper();

        try {
            mapper.writeValue(out,obj);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mapper = null ;

        }
    }

}
